﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MDIApplication.Classes;

namespace MDIApplication
{
    public partial class BookingDetails : Form
    {

        #region Form_Initialization
        ArrayList roomBooked;
        public BookingDetails(ArrayList selectedRooms, DateTime checkInDate, DateTime checkOutDate)
        {
            InitializeComponent();
            roomBooked = selectedRooms;
            foreach(Room r in selectedRooms)
            {
                label20.Text += r.roomUnitNo.ToString()+" ";
            }
            int yearSpan = DateTime.Now.Year - 18;
            dateTimePicker1.MaxDate = new DateTime(yearSpan, 1, 1);
            dateTimePicker2.Value = Convert.ToDateTime(checkInDate.Date.ToString());
            dateTimePicker2.MinDate = dateTimePicker2.Value;
            dateTimePicker3.Value = Convert.ToDateTime(checkOutDate.Date.ToString());
            TimeSpan daySpan=dateTimePicker3.Value - dateTimePicker2.Value;
            label21.Text = Convert.ToString(daySpan.Days);
            label27.Visible = false;
            textBox1.MaxLength = 9;
            textBox5.MaxLength = 8;
        }
        #endregion Form_Initialization

        #region createBooking Button
        AllBookingscs allBookingChild;
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == String.Empty || textBox2.Text == String.Empty || textBox3.Text == String.Empty || textBox4.Text == String.Empty || textBox5.Text == String.Empty)
            {
                MessageBox.Show("Pleasae fill up all required information.");
            }
            else
            {
                BookingRoomAvailabilityDBManager dbmgr = new BookingRoomAvailabilityDBManager();
                List<Guest> guestList = dbmgr.getAllGuests();
                Guest g = guestList.Last();

                //insert guest first

                Guest ng = new Guest();
                ng.icNo = textBox1.Text;
                ng.guestId = NxtKeyCode(g.guestId);
                ng.firstName = textBox2.Text;
                ng.lastName = textBox3.Text;
                ng.dateOfBirth = dateTimePicker1.Value;
                if (radioButton1.Checked == true)
                {
                    ng.gender = radioButton1.Text.ToString();
                }
                else if (radioButton2.Checked == true)
                {
                    ng.gender = radioButton2.Text.ToString();
                }
                ng.address = textBox4.Text;
                ng.contactNo = Convert.ToInt32(textBox5.Text);
                if (textBox6.Text == String.Empty)
                {
                    ng.email = "Null";
                }
                else
                {
                    ng.email = textBox6.Text;
                }
                BookingRoomAvailabilityDBManager db = new BookingRoomAvailabilityDBManager();


                foreach (Room r in roomBooked)
                {
                    //update room status
                    string statusUpdate = "Reserved";
                    int roomUnitNo = Convert.ToInt32(r.roomUnitNo);
                    db.updateRoomStatus(roomUnitNo, statusUpdate);

                    //insert booking
                    List<Booking> bookingList = dbmgr.getAllBookings();
                    Booking b = bookingList.Last();
                    Booking bk = new Booking();
                    bk.bookingId = NxtKeyCode(b.bookingId);
                    bk.checkInDate = dateTimePicker2.Value;
                    bk.checkOutDate = dateTimePicker3.Value;
                    bk.nightsOfStay = Convert.ToInt32(label21.Text);
                    bk.numberOfAdults = Convert.ToInt32(numericUpDown1.Value);
                    bk.numberOfChildren = Convert.ToInt32(numericUpDown2.Value);
                    if (textBox7.Text == String.Empty)
                    {
                        bk.additionalRemarks = "No";
                    }
                    else
                    {
                        bk.additionalRemarks = textBox7.Text;
                    }                    
                    bk.guestId = ng.guestId;
                    if (radioButton3.Checked == true)
                    {
                        bk.lateCheckOut = radioButton3.Text.ToString();
                    }
                    else if (radioButton4.Checked == true)
                    {
                        bk.lateCheckOut = radioButton4.Text.ToString();
                    }
                    bk.roomUnitNo = r.roomUnitNo;

                    //transfer to All Bookings Page
                    if (db.insertGuesttoDB(ng) == true && db.insertBookingtoDB(bk) == true)
                    {
                        MessageBox.Show("Booking Successful!");
                        CloseAllForms();
                        if (allBookingChild == null)
                        {
                            allBookingChild = new AllBookingscs();
                            allBookingChild.MdiParent = this.MdiParent;
                            allBookingChild.Location = new Point(0, 0);

                            allBookingChild.Show();
                            allBookingChild.Activate();
                        }
                    }
                }
            }
            
        }
        #endregion  createBooking Button

        #region AlphanumericIncrementForID
        public string NxtKeyCode(string KeyCode)
        {
            byte[] ASCIIValues = ASCIIEncoding.ASCII.GetBytes(KeyCode);
            int StringLength = ASCIIValues.Length;
            bool isAllZed = true;
            bool isAllNine = true;
            //Check if all has ZZZ.... then do nothing just return empty string.

            for (int i = 0; i < StringLength - 1; i++)
            {
                if (ASCIIValues[i] != 90)
                {
                    isAllZed = false;
                    break;
                }
            }
            if (isAllZed && ASCIIValues[StringLength - 1] == 57)
            {
                ASCIIValues[StringLength - 1] = 64;
            }

            // Check if all has 999... then make it A0
            for (int i = 0; i < StringLength; i++)
            {
                if (ASCIIValues[i] != 57)
                {
                    isAllNine = false;
                    break;
                }
            }
            if (isAllNine)
            {
                ASCIIValues[StringLength - 1] = 47;
                ASCIIValues[0] = 65;
                for (int i = 1; i < StringLength - 1; i++)
                {
                    ASCIIValues[i] = 48;
                }
            }


            for (int i = StringLength; i > 0; i--)
            {
                if (i - StringLength == 0)
                {
                    ASCIIValues[i - 1] += 1;
                }
                if (ASCIIValues[i - 1] == 58)
                {
                    ASCIIValues[i - 1] = 48;
                    if (i - 2 == -1)
                    {
                        break;
                    }
                    ASCIIValues[i - 2] += 1;
                }
                else if (ASCIIValues[i - 1] == 91)
                {
                    ASCIIValues[i - 1] = 65;
                    if (i - 2 == -1)
                    {
                        break;
                    }
                    ASCIIValues[i - 2] += 1;

                }
                else
                {
                    break;
                }

            }
            KeyCode = ASCIIEncoding.ASCII.GetString(ASCIIValues);
            return KeyCode;
        }
        #endregion AlphanumericIncrementForID

        #region cancelButton
        private void button2_Click(object sender, EventArgs e)
        {
            CloseAllForms();
            if (allBookingChild == null)
            {
                allBookingChild = new AllBookingscs();
                allBookingChild.MdiParent = this.MdiParent;
                allBookingChild.Location = new Point(0, 0);

                allBookingChild.Show();
                allBookingChild.Activate();
            }
        }
        #endregion cancleButton

        #region dateTimePicker
        private void dateTimePicker2_ValueChanged(object sender, EventArgs e)
        {
            dateTimePicker3.MinDate = dateTimePicker2.Value.AddDays(1);
            TimeSpan daySpan = dateTimePicker3.Value - dateTimePicker2.Value;
            label21.Text = Convert.ToString(daySpan.Days);
        }

        private void dateTimePicker3_ValueChanged(object sender, EventArgs e)
        {
            TimeSpan daySpan = dateTimePicker3.Value - dateTimePicker2.Value;
            label21.Text = Convert.ToString(daySpan.Days);
        }
        #endregion dateTimePicker

        #region CloseAllForms
        private void CloseAllForms()
        {
            Form[] formToClose = null;
            int i = 1;
            foreach (Form form in Application.OpenForms)
            {
                if (form != this && form.IsMdiChild) //this is form1
                {
                    Array.Resize(ref formToClose, i);
                    formToClose[i - 1] = form;
                    i++;
                }
            }
            if (formToClose != null)
            {
                for (int j = 0; j < formToClose.Length; j++)
                {
                    formToClose[j].Close();
                }
                allBookingChild = null;
            }
        }
        #endregion CloseAllForms

        #region textBox Validation
        private void textBox5_TextChanged(object sender, EventArgs e)
        {
            
            int num;
            bool isNum = int.TryParse(textBox5.Text.Trim(), out num);

            if (!isNum)
            {
                label27.Visible = true;
            }
            else
            {
                label27.Visible = false;
            }
        }


        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex IC = new System.Text.RegularExpressions.Regex(@"\b[A-Z]+([0-9][0-9][0-9][0-9][0-9][0-9][0-9])+[A-Z]$");
            if (textBox1.Text.Length > 0)
            {
                if (!IC.IsMatch(textBox1.Text))
                {
                    MessageBox.Show("Please enter vaild IC No.");
                    textBox1.SelectAll();
                    e.Cancel = true;
                }
            }
        }

        private void textBox6_Validating(object sender, CancelEventArgs e)
        {
            System.Text.RegularExpressions.Regex rEMail = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$");
            if (textBox6.Text.Length > 0)
            {
                if (!rEMail.IsMatch(textBox6.Text))
                {
                    MessageBox.Show("Please enter vaild E-mail address.");
                    textBox6.SelectAll();
                    e.Cancel = true;
                }
            }
        }

        #endregion textBox Validation

    }
}
